package com.vogue.supplymall.match.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.match.domain.model.MatchOrgan;
import com.vogue.supplymall.match.domain.service.MatchOrganService;
import com.vogue.supplymall.shop.domain.model.OrganShopImg;
import com.vogue.supplymall.shop.domain.service.OrganShopImgService;
import com.vogue.supplymall.shop.domain.service.ShopService;

@RestController
public class OrganController extends BaseController{

	@Resource
	private MatchOrganService matchOrganService;

	@Resource
	private ShopService shopService;
	
	@Resource
	private OrganShopImgService organShopImgService;
	
	/**
	 * 根据商城分类显示企业（供货商/卖家）
	 * 
	 * @param categoryCode	商城分类id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ns/{appver}/match/entlist/{categoryCode}", method = RequestMethod.POST)
	public ResponseEntity<List<Map<String,Object>>> entlist(
			@PathVariable("categoryCode") String categoryCode,
			HttpServletRequest request){
		List<Map<String,Object>> list = matchOrganService.findOrganByCategoryId(Long.parseLong(categoryCode));
		if(list == null) {
			list = new ArrayList<>();
		}			
		return new ResponseEntity<>(list, HttpStatus.OK);
	}

	/**
	 * 根据企业信息唯一号id来获取某个企业的简介
	 * 
	 * @param syscodeid	系统唯一号id（一个企业一个唯一号）
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ns/{appver}/match/entorganinfo/{syscodeid}", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> entorganinfo(
			@PathVariable("syscodeid") String syscodeid,
			HttpServletRequest request){
		Map<String,Object> map = new HashMap<>();
		map.put("info", "");
		MatchOrgan organ = matchOrganService.selectOneByWhr("type=1 and syscodeid='"+syscodeid+"'");
		if(organ != null){
			if(organ.getOrganinfo() != null) {
				map.put("info", organ.getOrganinfo());
			}		
		}
		return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
	}
	
	/**
	 * 根据企业信息唯一号id来获取某个企业的全部信息
	 * 
	 * @param syscodeid	系统唯一号id（一个企业一个唯一号）
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ns/{appver}/match/entorgan/{syscodeid}", method = RequestMethod.POST)
	public ResponseEntity<ControllerResultInfo> entorgan(@PathVariable("syscodeid") String syscodeid,
			HttpServletRequest request){
		ControllerResultInfo result = new ControllerResultInfo();
		MatchOrgan organ = matchOrganService.selectOneByWhr("type=1 and syscodeid='"+syscodeid+"'");
		if(organ != null){			
			result.setResult(true);
			OrganShopImg osi = organShopImgService.selectOneByWhr("imgtype=0 and organid=" + organ.getId());
			Map<String, Object> data= new HashMap<>();
			data.put("orgname", organ.getOrgname());
			data.put("organinfo", organ.getOrganinfo());
			data.put("id", organ.getId());
			data.put("syscodeid", organ.getSyscodeid());
			if(osi != null) {
				data.put("imgurl", osi.getImgurl());
			}			
			result.setData(data);	
		}else {
			throw new DataNotFindException("");
		}
		return new ResponseEntity<ControllerResultInfo>(result, HttpStatus.OK);
	}
}
